(function($, doc) {
	$.plusReady(function() {
		mui.init({
			gestureConfig: {
				tap: true, //默认为true
				doubletap: true, //默认为false
				longtap: true, //默认为false
				swipe: true, //默认为true
				drag: true, //默认为true
				hold: true, //默认为false，不监听
				release: true //默认为false，不监听
			}
		});
		var gpsPoint, map;
		init();
		function init(){
			plus.geolocation.getCurrentPosition(translatePoint, function(e) {
				mui.toast("异常:" + e.message);
			});
		}
		

		function translatePoint(position) {
			var currentLon = position.coords.longitude;
			var currentLat = position.coords.latitude;
			setMap(currentLon, currentLat);
		}

		function changeAdress(e) {
			map.clearOverlays();
			//map.addOverlay(new BMap.Marker(e.point.lng, e.point.lat));
			var currentLon = e.point.lng;
			var currentLat = e.point.lat;
			//setMap(e.point.lng, e.point.lat);
			gpsPoint = new BMap.Point(currentLon, currentLat);
			setlist();
			initMap(e.point);

		}

		function setMap(currentLon, currentLat) {
			gpsPoint = new BMap.Point(currentLon, currentLat);
			BMap.Convertor.translate(gpsPoint, 2, initMap); //坐标转换
			//mPoint = new BMap.Point(currentLon, currentLat);
			setlist();

		}

		function setlist() {
			var myGeo = new BMap.Geocoder(); //创建地址解析实例
			var mOption = {
				poiRadius: 1000, //半径为1000米内的POI,默认100米
				numPois: 100 //列举出50个POI,默认10个
			};
			myGeo.getLocation(gpsPoint,
				function mCallback(rs) {
					var allPois = rs.surroundingPois; //获取全部POI
					//console.log(JSON.stringify(allPois));
					document.getElementById("panel").innerHTML = template('map-template', {
						"list": allPois
					});
					mui.toast("请选择发送地址");
					/*
					for(i = 0; i < allPois.length; ++i) {
						document.getElementById("panel").innerHTML += "<li class=\"mui-table-view-cell mui-media mui-radio\">\n" +
							"<a class=\"mui-navigate\">" + allPois[i].title + "</a><p style=\"position: relative;white-space: nowrap;\">" + allPois[i].address + "</p></li>";
						//map.addOverlay(new BMap.Marker(allPois[i].point));
					}*/
				}, mOption
			);
		}

		function initMap(point) {
			map = new BMap.Map("allmap"); //创建地图
			map.addControl(new BMap.NavigationControl());
			map.addControl(new BMap.ScaleControl());
			map.addControl(new BMap.OverviewMapControl());
			map.centerAndZoom(point, 15);
			map.addOverlay(new BMap.Marker(point));
			map.addEventListener("click", changeAdress);
		}
		/*
		doc.getElementById('sendbtn').addEventListener("tap", function() {

		});*/
		mui('#panel').on('change', 'input', function() {
			var value = this.checked ? "true" : "false";
			var title = this.getAttribute('data-title');
			var lng = this.getAttribute('data-lng');
			var lat = this.getAttribute('data-lat');
			var address = this.getAttribute('data-address');
			//console.log(title);
			var btnArray = ['取消', '确定'];
			mui.confirm('你确定要发送该位置吗？', '发送位置', btnArray, function(e) {
				if(e.index == 1) {
					var chatbox = plus.webview.getWebviewById("chatbox");
					mui.fire(chatbox, 'SENDMAP', {
						title: title,
						address: address,
						lng: lng,
						lat: lat
					});
					var service_comfirm = plus.webview.getWebviewById("location.html");
					plus.webview.close(service_comfirm, "none");
					plus.webview.show('chatbox');
				} else {
					plus.nativeUI.toast('请再次选择位置');
				}
			})
		});
		var oft = plus.display.resolutionHeight;
		document.getElementById('maplist').style.height = oft - 340 + 'px';
		document.getElementById('searchInput').addEventListener('keyup', searchMap);

		function searchMap(e) {
			if(e.keyCode == 13) {
				var val = this.value;
				if(val != '') {
					var options = {
						onSearchComplete: function(results) {
							// 判断状态是否正确
							if(local.getStatus() == BMAP_STATUS_SUCCESS) {
								var s = [];
								for(var i = 0; i < results.getCurrentNumPois(); i++) {
									s.push(results.getPoi(i));
								}
								document.getElementById("panel").innerHTML = template('map-template', {
									"list": s
								});
								document.activeElement.blur(); 
							}else{ 
								mui.toast("位置不存在");
								init();
							}
						},
						renderOptions:{map: map}
					};
					var local = new BMap.LocalSearch(map, options);
					local.search(val);
				} else {
					mui.toast("请输入检索关键字");
					document.activeElement.blur();
					init();
				}
			}
		}
		// 百度地图API功能
		/*var point = new BMap.Point(116.331398,39.897445);
			map.centerAndZoom(point,12);
		
			var geolocation = new BMap.Geolocation();
			geolocation.getCurrentPosition(function(r){
				if(this.getStatus() == BMAP_STATUS_SUCCESS){
					var mk = new BMap.Marker(r.point);
					map.addOverlay(mk);
					map.panTo(r.point);
					alert('您的位置：'+r.point.lng+','+r.point.lat);
				}
				else {
					alert('failed'+this.getStatus());
				}        
			},{enableHighAccuracy: true})*/
		/*
			var map = new BMap.Map("allmap");
		var mPoint = new BMap.Point(112.9911150572, 28.1652184608);
		//            var mPoint = new BMap.Point(112.97935279,28.21347823);
		map.centerAndZoom(mPoint, 16);
		map.enableScrollWheelZoom(); //启用滚轮缩放

		var mOption = {
			poiRadius: 1000, //半径为1000米内的POI,默认100米
			numPois: 50 //列举出50个POI,默认10个
		}
		var myGeo = new BMap.Geocoder(); //创建地址解析实例
		function displayPOI() {
			myGeo.getLocation(mPoint,
				function mCallback(rs) {
					var allPois = rs.surroundingPois; //获取全部POI
					console.log(allPois);
					for(i = 0; i < allPois.length; ++i) {
						document.getElementById("panel").innerHTML += "<li class=\"mui-table-view-cell mui-media\">\n" +
							"<a class=\"mui-navigate\">" + allPois[i].title + "</a><p style=\"position: relative;white-space: nowrap;\">" + allPois[i].address + "</p></li>";
						map.addOverlay(new BMap.Marker(allPois[i].point));
					}
				}, mOption
			);
		}
		displayPOI();*/
	});
}(mui, document));